﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Threading.Tasks;
using Demo.Entity;

namespace Demo.Helper
{
    public class UserHelper
    {
        private string _filePath = string.Empty;

        public UserHelper(string path)
        {
            _filePath = path;
        }

        /// <summary>
        /// 序列化用户信息到文件
        /// </summary>
        /// <param name="path">文件路径</param>
        /// <param name="listUser">用户对象列表</param>
        /// <returns></returns>
        public bool SerialzedUser(string path, List<User> listUser)
        {
            if (listUser == null)
            {
                return false;
            }
            BinaryFormatter format = new BinaryFormatter();
            using (FileStream fs = new FileStream(path, FileMode.Create, FileAccess.Write))
            {
                format.Serialize(fs, listUser);
                return true;
            }
        }

        /// <summary>
        /// 反序列化文件到用户信息对象
        /// </summary>
        /// <param name="path">文件路径</param>
        /// <returns></returns>
        public List<User> DeserializedUser(string path)
        {
            BinaryFormatter format = new BinaryFormatter();
            try
            {
                using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read))
                {
                    object o = format.Deserialize(fs);
                    return (List<User>)o;
                }
            }
            catch (Exception)
            {
                return null;
            }
        }

        /// <summary>
        /// 添加超级用户
        /// </summary>
        /// <param name="path"></param>
        /// <param name="listUser"></param>
        public void CheckSuperUser(string path, List<User> listUser)
        {
            if (!File.Exists(path))
            {
                User user = new User()
                {
                    Id = 0,
                    Name = "Admin",
                    PassWord = "Admin123",
                    Level = Authority.管理员,
                };
                listUser.Add(user);
                SerialzedUser(path, listUser);
            }
        }

        /// <summary>
        /// 检查重复性
        /// </summary>
        /// <param name="listUser"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        public bool CheckContainUser(List<User> listUser, string name)
        {
            var user = from item in listUser where item.Name == name select item;
            if (user.Count() > 0)
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// 添加用户
        /// </summary>
        /// <param name="path"></param>
        /// <param name="listUser"></param>
        /// <param name="user"></param>
        /// <returns></returns>
        public bool AddUser(string path, List<User> listUser, User user)
        {
            if (user == null)
                return false;
            if (CheckContainUser(listUser, user.Name))
                return false;
            listUser.Add(user);
            SerialzedUser(path, listUser);
            return true;
        }

        /// <summary>
        /// 删除用户
        /// </summary>
        /// <param name="path"></param>
        /// <param name="listUser"></param>
        /// <param name="user"></param>
        /// <returns></returns>
        public bool DeleteUser(string path, List<User> listUser, string username)
        {
            if (listUser == null)
                return false;
            int Id = 0;
            foreach (User item in listUser)
            {
                if (item.Name == username)
                {
                    break;
                }
                Id++;
            }
            if (Id == 0)
                return false;
            listUser.RemoveAt(Id);
            SerialzedUser(path, listUser);
            return true;
        }
    }
}
